﻿using System;
using System.Windows;
using System.Windows.Browser;

namespace GSSoftSL.Utility
{
    public static class UtilityFunction
    {
        public static Guid GetCurrentUserId()
        {
            return WebContext.Current.User.UserId;
            //return new Guid("48F27C56-0DC4-4609-A6B0-B9BA83A7FAAF");
        }

        public static void ShowReportPopup(string PopupURL, int PopupWidth, int PopupHeight)
        {
            // From: http://sushantp.wordpress.com/2009/11/25/silverlight-reporting-support-for-ssrs-reports-problem-and-possible-solutions/
            //if (HtmlPage.IsPopupWindowAllowed)
            //{
                System.Text.StringBuilder codeToRun = new System.Text.StringBuilder();
                codeToRun.Append("window.open(");
                codeToRun.Append("\"");
                codeToRun.Append(string.Format("{0}", PopupURL));
                codeToRun.Append("\",");
                codeToRun.Append("\"");
                codeToRun.Append("\",");
                codeToRun.Append("\"");
                codeToRun.Append("width=" + PopupWidth.ToString() + ",height=" + PopupHeight.ToString());
                codeToRun.Append(",scrollbars=no,menubar=no,toolbar=no,resizable=no,directories=no,titlebar=no,location=no,status=no");
                // 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=350'
                codeToRun.Append("\");");
                try
                {
                    HtmlPage.Window.Eval(codeToRun.ToString());
                }
                catch
                {
                    MessageBox.Show("You must enable popups to view reports. Safari browser is not supported.",
                        "Error", MessageBoxButton.OK);
                }
            /*}
            else
            {
                MessageBox.Show("You must enable popups to view reports. Safari browser is not supported.",
                    "Error", MessageBoxButton.OK);
            }*/
        }

        public static void ShowReportDialog(string URL, int PopupWidth, int PopupHeight)
        {
            // From: http://sushantp.wordpress.com/2009/11/25/silverlight-reporting-support-for-ssrs-reports-problem-and-possible-solutions/
            //if (HtmlPage.IsPopupWindowAllowed)
            //{
            System.Text.StringBuilder codeToRun = new System.Text.StringBuilder();
            /*codeToRun.Append("window.open(");
            codeToRun.Append("\"");
            codeToRun.Append(string.Format("{0}", PopupURL));
            codeToRun.Append("\",");
            codeToRun.Append("\"");
            codeToRun.Append("\",");
            codeToRun.Append("\"");
            codeToRun.Append("width=" + PopupWidth.ToString() + ",height=" + PopupHeight.ToString());
            codeToRun.Append(",scrollbars=no,menubar=no,toolbar=no,resizable=no,directories=no,titlebar=no,location=no,status=no");
            codeToRun.Append("\");");*/
            codeToRun.Append(string.Format("$('#dialog-report').dialog('open').find('iframe').attr('src', '{0}')", URL));
            try
            {
                HtmlPage.Window.Eval(codeToRun.ToString());
            }
            catch
            {
                MessageBox.Show("You must enable popups to view reports. Safari browser is not supported.",
                    "Error", MessageBoxButton.OK);
            }
        }

        public static Uri GetBaseAddress()
        {
            // Get the web address of the .xap that launched this application     
            string strBaseWebAddress = App.Current.Host.Source.AbsoluteUri;
            // Find the position of the ClientBin directory        
            int PositionOfClientBin =
                App.Current.Host.Source.AbsoluteUri.ToLower().IndexOf(@"/clientbin");
            // Strip off everything after the ClientBin directory         
            strBaseWebAddress = strBaseWebAddress.Substring(0, PositionOfClientBin);//Strings.Left(strBaseWebAddress, PositionOfClientBin);
            // Create a URI
            Uri UriWebService = new Uri(String.Format(@"{0}/Report.aspx?", strBaseWebAddress));
            // Return the base address          
            return UriWebService;
        }
    }
}
